package com.topscomm.mpm.script;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectAppointmentEntity;
import com.topscomm.mpm.pojo.MpmProjectChangeEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectTeamEntity;
import com.topscomm.mpm.service.impl.MpmProjectAppointmentService;
import com.topscomm.mpm.service.impl.MpmProjectChangeService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.mpm.service.impl.MpmProjectTeamService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wudaliang
 * @date 2021/7/16
 * @description:
 * @modify: liuchenguang 2021-8-19 删除相关方更新
 */
public class MpmProjectChangeScript extends MpmScript {

    @Transactional
    public void projectChangeUpdate(ITapService service, TapEntity entity) throws Exception{
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        MpmProjectChangeService mpmProjectChangeService= (MpmProjectChangeService)SpringUtil.getBean("mpmProjectChangeService");

        MpmProjectChangeEntity mpmProjectChangeEntity = mpmProjectChangeService.queryById(sourceid);
        String projectcode2 = mpmProjectChangeEntity.getProjectcode2();
        mpmProjectChangeService.projectChangeUpdate(projectcode2,mpmProjectChangeEntity);
    }

    /**
     * @author houyuhang
     * @date 2023-02-03
     * @description: 新增立项结束，给项目组成员发送消息
     * @modify:
     */
    @Transactional
    public void projectChangeSendMessage(ITapService service, TapEntity entity) throws Exception {
        // 审批点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        MpmProjectTeamService mpmProjectTeamService = (MpmProjectTeamService) SpringUtil.getBean("mpmProjectTeamService");
        MpmProjectChangeService mpmProjectChangeService= (MpmProjectChangeService)SpringUtil.getBean("mpmProjectChangeService");
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        // 获取项目立项信息
        MpmProjectChangeEntity mpmProjectChangeEntity = mpmProjectChangeService.queryById(sourceid);
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("projectcode = '{0}'", mpmProjectChangeEntity.getProjectcode2());
        MpmProjectEntity mpmProjectEntity = mpmProjectService.queryObjectByWhere(sql.toString());
        // 消息内容（具体变更项）
        String contentInfo = "立项";
        // 给项目经理发送变更审核通过消息
        if (!MpmSystemConst.ChangeNeedApprovalLine.NoLine.equals(mpmProjectChangeEntity.getNeedapprovalline())) {
            mpmProjectTeamService.sendMessageToPM(sourceid, sourcetype, contentInfo, mpmProjectEntity);
        }
        // 提醒项目PMO和省区PMO立项变更的内容
        mpmProjectChangeService.sendMessageChangeToPMO(mpmProjectChangeEntity, mpmProjectEntity);
    }
}
